//
//  HtestCollectionViewCell.m
//  NursingManages
//
//  Created by Zhen Huang on 17/9/15.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HtestCollectionViewCell.h"
#import "HAnswerTableViewCell.h"
#import "DIHTest.h"
#import "Model_Hr_Question_Type.h"
#import "SizeToFit.h"
#import "UtilsMacro.h"
#import "HTestViewController.h"
@interface HtestCollectionViewCell()

@property (nonatomic, strong) Model_Hr_Question_Item *model;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) CGFloat rowHeight;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSMutableArray *arrays;
@property (nonatomic, copy) NSString *typeid;
@property (nonatomic, strong) UIView *footerview;


@end

@implementation HtestCollectionViewCell


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.contentView.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.label];
        [self.contentView addSubview:self.tableview];
        self.arrays = [NSMutableArray new];
        [self addSubview:self.rightImage];
        [self addSubview:self.wrongImage];
        [self addSubview:self.rightLabel];
        [self addSubview:self.wrongLabel];
        
    }
    
    return self;
}
- (void)configure:(NSInteger)index
        dataArray:(NSArray *)dataArray
       headHeight:(CGFloat)height
        headTitle:(NSString *)title
           typeid:(NSString *)type_id{
    
    Model_Hr_Question_Item *model = dataArray[index];
    self.model = model;
    self.rows = index;
    self.height = height;
    self.title = title;
    self.typeid = type_id;
    self.dataArray = dataArray;
    self.label.text = [NSString stringWithFormat:@"%ld/%ld",index + 1,dataArray.count];
    [self.tableview reloadData];

}

- (UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth - 80, ScreenHeight - 120, 80, 80)];
//        _label.backgroundColor = [UIColor blueColor];
        _label.textColor = [UIColor orangeColor];
        _label.textAlignment = NSTextAlignmentCenter;
        
    }
    return _label;
}

- (UIImageView *)rightImage
{
    if (!_rightImage) {
        _rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, ScreenHeight - 90, 20, 20)];
        _rightImage.image = [UIImage imageNamed:@"icon_correct3"];
    }
    return _rightImage;
}
- (UILabel *)rightLabel
{
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.rightImage.right + 5, ScreenHeight - 93, 25, 25)];
        _rightLabel.text = @"0";
        _rightLabel.textColor = RGB(69, 185, 124);
    }
    return _rightLabel;
}

- (UIImageView *)wrongImage
{
    if (!_wrongImage) {
        _wrongImage = [[UIImageView alloc]initWithFrame:CGRectMake(70, ScreenHeight - 90, 20, 20)];
        _wrongImage.image = [UIImage imageNamed:@"icon_wrong"];
    }
    return _wrongImage;
}
- (UILabel *)wrongLabel
{
    if (!_wrongLabel) {
        _wrongLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.wrongImage.right + 5, ScreenHeight - 93, 25, 24)];
        _wrongLabel.text = @"0";
        _wrongLabel.textColor = [UIColor redColor];
    }
    return _wrongLabel;
}

- (UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.tableFooterView = [[UIView alloc]init];
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableview registerClass:[HAnswerTableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableview;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.optionArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HAnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (self.model.answers != nil) {
        cell.userInteractionEnabled = NO;

    }else {
        cell.userInteractionEnabled = YES;
    }
    
    NSString *option = self.model.optionArray[indexPath.row];
        switch (indexPath.row) {
        case 0:
            option = [NSString stringWithFormat:@"A.%@",option];
            break;
        case 1:
            option = [NSString stringWithFormat:@"B.%@",option];
            break;
        case 2:
            option = [NSString stringWithFormat:@"C.%@",option];
            break;
        case 3:
            option = [NSString stringWithFormat:@"D.%@",option];
            break;
        case 4:
            option = [NSString stringWithFormat:@"E.%@",option];
            break;
        case 5:
            option = [NSString stringWithFormat:@"F.%@",option];
            break;
        case 6:
            option = [NSString stringWithFormat:@"G.%@",option];
            break;
        case 7:
            option = [NSString stringWithFormat:@"H.%@",option];
            break;
        case 8:
            option = [NSString stringWithFormat:@"I.%@",option];
            break;
        case 9:
            option = [NSString stringWithFormat:@"J.%@",option];
            break;

        default:
            break;
    }
    cell.selectlable.text = [NSString stringWithFormat:@"%@",option];
    cell.selectlable.numberOfLines = 0;
    cell.selectlable.adjustsFontSizeToFitWidth = YES;
    CGFloat height = [SizeToFit getHeightByWidth:ScreenWidth - 20 title:cell.selectlable.text font:[UIFont systemFontOfSize:16]];
    cell.selectlable.frame = CGRectMake(cell.selectedBtn.right + 10, 15, ScreenWidth - cell.selectedBtn.right - 20, height);
    
//    if (model.isSelected) {
    [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"weixuanzhong"] forState:UIControlStateNormal];
        
//    } else {
//        [self.selectButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong"] forState:UIControlStateNormal];
//    }
//
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HAnswerTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@ %ld",self.dataArray,self.rows);
    Model_Hr_Question_Item *model = self.dataArray[self.rows];
    [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"xuanzhong"] forState:UIControlStateNormal];
    
       NSString *answer = @"";
    switch (indexPath.row) {
        case 0:
            answer = @"A";
            break;
        case 1:
            answer = @"B";
            break;
        case 2:
            answer = @"C";
            break;
        case 3:
            answer = @"D";
            break;
        case 4:
            answer = @"E";
            break;
        case 5:
            answer = @"F";
            break;
        case 6:
            answer = @"G";
            break;
        case 7:
            answer = @"H";
            break;
        case 8:
            answer = @"I";
            break;
        case 9:
            answer = @"J";
            break;

            
        default:
            break;
    }
    if ([model.TYPEID isEqualToString:@"2"] || [model.TYPEID isEqualToString:@"5"]) {
        [self.arrays addObject:answer];
        
    }else {
        model.answers = answer;
        DIHTest *test = [[DIHTest alloc]init];
        [test modelItem:model];

        if ([answer isEqualToString:model.ANSWER]) {

            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, ScreenWidth - 20, 25)];
            label.text = @"回答正确";
            [self.footerview addSubview:label];
            self.rightLabel.text = [NSString stringWithFormat:@"%ld",[self.rightLabel.text integerValue] + 1];
        }else {
            NSLog(@"错了");
            self.wrongLabel.text = [NSString stringWithFormat:@"%ld",[self.wrongLabel.text integerValue] + 1];
        }
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth , 40)];
    [headView addSubview:label];
    
    label.text = [NSString stringWithFormat:@"%@",self.title];
    label.numberOfLines = 0;
    label.frame = CGRectMake(10, -10 ,ScreenWidth - 20, self.height);

    return headView;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    self.footerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
//    self.footerview.backgroundColor = [UIColor redColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 30, ScreenWidth - 20, 40);
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 7;
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.backgroundColor = kNavBlueColor;
    [button addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    if ([self.typeid isEqualToString:@"2"] || [self.typeid isEqualToString:@"5"]) {
        [self.footerview addSubview:button];
    }
    return self.footerview;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.height - 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([self.typeid isEqualToString:@"2"] || [self.typeid isEqualToString:@"5"]) {
        return 70;
    }else {
        return 30;
    }
}
- (void)sure:(UIButton *)sender
{
    Model_Hr_Question_Item *model = self.dataArray[self.rows];
    for (NSString *string in self.arrays) {
        NSLog(@"====%@",string);
    }

}
@end
